<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/12/23
 * Time: 10:43
 */
include_once "../vendor/autoload.php";
set_time_limit(0);
ini_set('memory_limit', '1500M');

use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use QL\QueryList;

class LanrenCollect
{
    private $base_url;

    private $inCharset;

    private $outCharset;

    private $client;

    public function __construct($inCharset = '', $outCharset = '')
    {
        //
        $this->base_url = 'http://www.lanrenzhijia.com/';
        $this->inCharset = $inCharset;
        $this->outCharset = $outCharset;
        $this->client = new Client();
    }

    /**
     * @param $uri String URL
     * @return mixed|\Psr\Http\Message\ResponseInterface
     */
    public function getContent($url)
    {
        return $this->client->request('GET', $url);
    }

    public function process($category=null)
    {
        //获取所有分类
        if (is_null($category)) {
            die('not found category!');
        }

        //获取分类所有页面
        foreach ($category as $key => $value) {
            $pageLists[$value]['uri'] = $value;
            $pageLists[$value]['pagelist'] = $this->getPageList($this->base_url . $value);
        }

        //获取分类所有文章url
        foreach ($pageLists as &$value) {
            if (false == $this->getArticles($value)) {
                continue;
            }
            $value['articles'] = $this->getArticles($value);

            if ($this->putToDB($value)) {
                unset($value);
            }
        }
        var_dump($pageLists);die;

    #抓取具体内容考虑为第二步骤，从数据库获取后再抓取 || 直接从这里开始抓取
        //抓取文章内容放入redis(标题、简介、缩略图path、资源path)
        while (! $this->MachineStatus()) {//判断CPU 和 内存使用情况，超标时不再操作redis,暂停休息，等待队列任务完成再放入redis
            sleep(5);
            echo "the MachineStatus was not good, please waite a moment.\n";
        }

    }

    /**
     * @param $arr 分类所有文章信息数组
     */
    public function putToDB($arr)
    {
        $result = $this->getInsertData($arr);

        if (! DB::insert($result)) {
            return false;
        }
        return true;
    }
    /**
     * @return bool 检测机器内存和CPU状态是否超标
     */
    public function MachineStatus()
    {
        if (memory_get_usage() > 1500000000 || memory_get_peak_usage() > 1500000000) {
//            die("memory was overflow"); #record log
            return false;
        }

//        if (getrusage() > 50) { #windows does not support
//            die("CPU was tired"); #record log
//            return false
//        }
    }

//    public function getCategory()
//    {
//        $_content = $this->getContent($this->base_url);
//        $content = $_content->getBody()->getContents();
//
//        //匹配出所有分类的URI
//        $rules = [
//            'category'  =>  ['#menu>ul>li>a', 'href']
//        ];
//
//        $response = QueryList::Query($content, $rules)->getData();
//
//        var_dump($response);die;
//    }

    /**
     * @param $url 分类URL
     * @return array 该分类所有页面uri集合
     */
    public function getPageList($url)
    {
        $_content = $this->getContent($url);
        $content = $_content->getBody()->getContents();

        //匹配出所有分类的分页数
        $rules = [
            'pageNums'  =>  ['.pageinfo>strong:eq(0)', 'text']
        ];

        $pageNums = QueryList::Query($content, $rules)->getData();

        $pageUri = '';
        $pageLists = [];

        $pageRules = [
            'pageRules' =>  ['.pagelist>li>a:eq(0)', 'href'],
        ];
        $_pageList = QueryList::Query($content, $pageRules)->getData();

        $pageUri = mb_substr($_pageList[0]['pageRules'], 0, -6);

        for ($i = 1; $i <= $pageNums[0]['pageNums']; $i++) {
            $pageLists[] = $pageUri . $i . '.html';
        }
        return $pageLists;
    }

    public function getArticles($pagelist)
    {
        if (! count($pagelist['pagelist'])) {
            #record log
            return false;
        }
        $rules = [
            'articles'  =>  ['.title', 'href']
        ];
        foreach ($pagelist['pagelist'] as $value) {
//            $pagelist['articles'][] = $this->base_url . $pagelist['uri'] . $value;
            $_content = $this->getContent($this->base_url . $pagelist['uri'] . $value);
            $content = $_content->getBody()->getContents();

            $rs = QueryList::Query($content, $rules)->getData();

            foreach ($rs as $k => $val) {
                $pagelist['articles'][] = $rs[$k]['articles'];
            }

        }

        return $pagelist['articles'];
    }

    public function getFile()
    {

    }

}

$app = new LanrenCollect();

//$app->getPageList('http://www.lanrenzhijia.com/js/list_12_1.html');
$category = [
//    'flash/',
    'nav/',
    'time/'
];
$app->process($category);